﻿using Sundry.IDAL;
using Sundry.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sundry.DAL
{
    public class BrandRepository : BaseRepository<Brand>, IBrandRepository
    {
        /// <summary>
        /// 获取品牌列表
        /// </summary>
        /// <param name="brandName">品牌名称</param>
        /// <param name="nowNum">当前最大数字</param>
        /// <param name="pageSize">长度</param>
        /// <param name="rowCount">总数</param>
        /// <returns>品牌列表</returns>
        public IQueryable<dynamic> LoadBradnList(string brandName, int nowNum, int pageSize, out int rowCount)
        {
            var result = from p in db.Set<Brand>()
                         orderby
                          p.OrderNum ascending
                         select new
                         {
                             p.Id,
                             p.Intro,
                             p.IsRec,
                             p.IsShow,
                             p.Logo,
                             p.Name,
                             p.OrderNum,
                             Sid=p.Id
                         };
            if (!string.IsNullOrEmpty(brandName))
                result = result.Where(m => m.Name.Contains(brandName));
            rowCount = result.Count();
            result = result.Skip(nowNum).Take(pageSize);
            return result;
        }

        /// <summary>
        /// 查找符合条件的第一个品牌
        /// </summary>
        /// <param name="whereLamda">查询条件</param>
        /// <returns>符合条件的第一个品牌</returns>
        public Brand SelectFirstBrand(Expression<Func<Brand, bool>> whereLamda)
        {
            return db.Set<Brand>().Where(whereLamda).FirstOrDefault();
        }
    }
}
